package controllers

import (
	"errmsg"
	"errors"
	"github.com/astaxie/beego/logs"
	"github.com/astaxie/beego/validation"
	"smart_home/api/models"
)

type DelayController struct {
	BaseController
}

// @Title Create
// @Description 添加延时任务
// @router / [post]
func (c *DelayController) Post() {
	var (
		dl     *models.Delay
		err    error
		vali   validation.Validation
		isAuth bool
	)

	dl = &models.Delay{State: 1}
	if err = c.ParseData(dl); err != nil {
		logs.Error(err)
		c.Error(errmsg.ErrResponseParameter)
		return
	}

	vali = validation.Validation{}
	vali.Required(dl.Sta, "sta").Message("sta不能为空")
	vali.Range(dl.Start, 0, 1440, "start").Message("打开时间取值0-1440之间")
	vali.Range(dl.End, 0, 1440, "end").Message("打开时间取值0-1440之间")
	vali.Range(dl.Order, 0, 1, "order").Message("order取值为0,1")
	if vali.HasErrors() {
		c.Error(vali.Errors[0])
		return
	}

	if dl.Start == 0 && dl.End == 0 {
		err = errors.New("打开时间和关闭时间至少填写一项")
		logs.Info(err)
		c.Error(err)
		return
	}

	if models.IsExistsDelayBySta(dl.Sta) {
		err = errors.New("同时只能有一个延时任务")
		logs.Info(err)
		c.Error(err)
		return
	}

	if isAuth, err = models.IsEquipmentAuth(c.User.Id, dl.Sta); err != nil {
		logs.Info(err)
		c.Error(errmsg.ErrResponseSystem)
		return
	}
	if !isAuth {
		c.Error(errmsg.ErrResponseAuth)
		return
	}

	if !models.AddDelay(dl) {
		c.Error(errors.New("添加失败，请检查设备是否已经联网"))
		return
	}

	c.Success(nil)
}

// @Title Get
// @Description 获取延时任务
// @param sta path false "设备sta"
// @router /:sta [get]
func (c *DelayController) Get() {
	var (
		sta string
		dl  *models.Delay
		err error
	)
	sta = c.GetString(":sta")
	if dl, err = models.GetDelayBySta(sta); err != nil {
		c.Error(errmsg.ErrResponseSystem)
		return
	}
	c.Success(dl)
}

// @Title Create
// @Description 修改定时任务
// @router /:id [put]
func (c *DelayController) Put() {
	var (
		id     string
		err    error
		dl     *models.Delay
		isAuth bool
		vali   validation.Validation
	)

	id = c.GetString(":id")
	if dl, err = models.GetDelayById(id); err != nil {
		c.Error(errmsg.ErrResponseSystem)
		return
	}
	if dl == nil {
		c.Error(errmsg.ErrResponseNotFound)
		return
	}

	if isAuth, err = models.IsEquipmentAuth(c.User.Id, dl.Sta); err != nil {
		c.Error(errmsg.ErrResponseSystem)
		return
	}
	if !isAuth {
		c.Error(errmsg.ErrResponseAuth)
		return
	}

	if err = c.ParseData(dl); err != nil {
		logs.Error(err)
		c.Error(errmsg.ErrResponseSystem)
		return
	}

	vali = validation.Validation{}
	vali.Required(dl.Sta, "sta").Message("sta不能为空")
	vali.Range(dl.Start, 0, 1440, "start").Message("打开时间取值0-1440之间")
	vali.Range(dl.End, 0, 1440, "end").Message("打开时间取值0-1440之间")
	vali.Range(dl.State, 0, 1, "state").Message("state取值为0,1")
	vali.Range(dl.Order, 0, 1, "order").Message("order取值为0,1")
	if vali.HasErrors() {
		logs.Info(vali.Errors[0])
		c.Error(vali.Errors[0])
		return
	}

	if dl.Start == 0 && dl.End == 0 {
		err = errors.New("打开时间和关闭时间至少填写一项")
		logs.Info(err)
		c.Error(err)
		return
	}

	if !models.UpdateDelay(dl) {
		c.Error(errors.New("修改失败，请检查设备是否已经联网"))
		return
	}

	c.Success(nil)
}

// @Title Delete
// @Description 删除设备
// @Param	id		path 	string	true		"sta"
// @router /:id [delete]
func (c *DelayController) Delete() {
	var (
		id     string
		err    error
		dl     *models.Delay
		isAuth bool
	)

	id = c.GetString(":id")
	if dl, err = models.GetDelayById(id); err != nil {
		c.Error(errmsg.ErrResponseSystem)
		return
	}
	if dl == nil {
		c.Error(errmsg.ErrResponseNotFound)
		return
	}

	if isAuth, err = models.IsEquipmentAuth(c.User.Id, dl.Sta); err != nil {
		c.Error(errmsg.ErrResponseSystem)
		return
	}
	if !isAuth {
		c.Error(errmsg.ErrResponseAuth)
		return
	}

	if !models.DelDelay(dl) {
		c.Error(errors.New("删除失败，请检查设备是否已经联网"))
		return
	}

	c.Success(nil)
}
